'''
Runnable组件动态添加默认调用参数
4. configurable_fields运行流程与解析
'''
'''
Runnable 组件重试 / 回退机制
- 回退机制
'''

import dotenv
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

dotenv.load_dotenv()

mpt = ChatPromptTemplate.from_template('{q}')
# 模拟指定两次不存在的模型， 通过with_fallbacks自动回退
llm = ChatOpenAI(model = "gpt-4k").with_fallbacks([ChatOpenAI(model = "gpt-3.5o"), ChatOpenAI(model = "gpt-4o")])

chain = mpt | llm | StrOutputParser()
content = chain.invoke({'q': '你是Gpt几？'})
print(content)